from rocketmq import PushConsumer


def main():
    # 创建消费者实例
    consumer = PushConsumer("your_consumer_group")
    consumer.set_name_server_address("your_namesrv_address")

    # 订阅主题和 tag
    consumer.subscribe("your_topic", "*")

    # 注册消息回调函数
    @consumer.register_callback
    def on_message(msg):
        print(f"Received message: {msg.body}, Key: {msg.key}")
        return True  # 或 False 表示消息重试

    # 启动消费者
    consumer.start()
    print("Consumer started.")

    # 保持程序运行
    input("Press any key to exit.\n")

    # 关闭消费者
    consumer.shutdown()


if __name__ == "__main__":
    main()